package com.zhongen.wflow.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : willian fu
 * @date : 2022/9/12
 */
@Configuration
public class AsyncTaskTheadPoolConfig {

	/**
	 * 核心线程数 默认的核心线程数为1
	 *
	 */
	private static final int CORE_POOL_SIZE = 5;

	/**
	 * 最大线程数 默认的最大线程数是Integer.MAX_VALUE 即2<sup>31</sup>-1
	 */
	private static final int MAX_POOL_SIZE = 1500;

	/**
	 * 缓冲队列数 默认的缓冲队列数是Integer.MAX_VALUE 即2<sup>31</sup>-1
	 */
	private static final int QUEUE_CAPACITY = 4000;

	/**
	 * 允许线程空闲时间 默认的线程空闲时间为60秒
	 */
	private static final int KEEP_ALIVE_SECONDS = 60;

	/**
	 * 线程池前缀名
	 */
	private static final String THREAD_NAME_PREFIX = "Task_Service_Async_";

	/**
	 * allowCoreThreadTimeOut为true则线程池数量最后销毁到0个 allowCoreThreadTimeOut为false
	 * 销毁机制：超过核心线程数时，而且（超过最大值或者timeout过），就会销毁。 默认是false
	 */
	private boolean allowCoreThreadTimeOut = false;

	public static ThreadPoolTaskScheduler taskScheduler;

	@Bean
	public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
		taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
		taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
		taskExecutor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
		taskExecutor.setThreadNamePrefix(THREAD_NAME_PREFIX);
		taskExecutor.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
		taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		// 线程池初始化
		taskExecutor.initialize();

		ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
		taskScheduler = threadPoolTaskScheduler;
		threadPoolTaskScheduler.setThreadFactory(taskExecutor);
		return threadPoolTaskScheduler;
	}

	@Bean
	public ExecutorService getExecutorService() {
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
		taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
		taskExecutor.setQueueCapacity(QUEUE_CAPACITY);
		taskExecutor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
		taskExecutor.setThreadNamePrefix(THREAD_NAME_PREFIX);
		taskExecutor.setAllowCoreThreadTimeOut(allowCoreThreadTimeOut);
		taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		// 线程池初始化
		taskExecutor.initialize();

		return Executors.newCachedThreadPool(taskExecutor);
	}

}
